﻿$(function () {

    var tempLoadingInterval = null, tempLoadingNum = 1;

    $.globalIds = {
        tempLoading: '#tempLoadingContain',
        tempWindow: '#tempWindow',
        tempFormInWindow: '#tempWindow form'
    };

    $.globalCookieNames = {
        setupWizard: {
            keywords: 'SetupWizrd_Keywords'
        }
    };

    $.enums = {
        interactive: {
            starting: 0,
            end: 1,
            timeout: 2
        },
        taskStatus: {
            stop: 0,
            running: 1,
            suspend: 2,
            normalEnd: 3
        }
    };
    
    $.fn.serializeObject = function () {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function () {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };

    $.extend({
        removePrefix: function (idStr) {
            if (idStr && idStr.indexOf('#') == 0)
                return idStr.substring(1, idStr.length);
            return idStr;
        },
        showLoading: function (msg, canCustomClose) {
            if (!$.ui || !$.ui.dialog) return;
            if (msg == undefined)
                msg = "Loading";
            if (canCustomClose == undefined)
                canCustomClose = true;
            var $TempLoading = $($.globalIds.tempLoading);
            if ($TempLoading.size() == 0)
                $TempLoading = $("<div id='" + $.removePrefix($.globalIds.tempLoading) + "'>" + msg + '</div>');
            tempLoadingNum = 1;
            clearInterval(tempLoadingInterval);
            tempLoadingInterval = setInterval(function () {
                var showMsg = msg;
                for (var t = 0; t < tempLoadingNum; t++) {
                    showMsg += '.';
                }
                tempLoadingNum += (tempLoadingNum == 6 ? -5 : 1);
                $TempLoading.text(showMsg);
            }, 1000);
            $TempLoading.css('text-align', 'center');
            $TempLoading.dialog({
                closeOnEscape: false,
                dialogClass: 'tempLoadingDialog',
                draggable: false,
                resizable: false,
                modal: true,
                autoOpen: true,
                title: 'Loading now',
                width: 250,
                height: 100,
                open: canCustomClose && function () {
                    $(this).parent('.ui-dialog').find('.ui-dialog-titlebar-close').hide();
                }
            });
        },
        hideLoading: function () {
            if (!$.ui || !$.ui.dialog) return;
            var $TempLoading = $($.globalIds.tempLoading);
            $TempLoading.dialog('close');
            if (tempLoadingInterval)
                clearInterval(tempLoadingInterval);
        },
        openWindow: function (options) {
            if (!$.ui || !$.ui.dialog) return;
            var defaults = {
                modal: true,
                draggable: true,
                resizable: true,
                autoOpen: true,
                closeOnEscape: false,
                width: 640,
                height: 720,
                minWidth: options.minWidth || options.width,
                minHeight: options.minHeight || options.height,
                loadedFun: undefined
            };
            $.extend(defaults, options);
            var $TempWindow = $($.globalIds.tempWindow);
            if ($TempWindow.size() == 0)
                $TempWindow = $("<div id='" + $.removePrefix($.globalIds.tempWindow) + "'></div>").appendTo(document.body);
            $TempWindow.load(defaults.url, function () {
                if (defaults.loadedFun)
                    defaults.loadedFun.call();
                $TempWindow.dialog(defaults);
            });
        },
        closeWindow: function () {
            if (!$.ui || !$.ui.dialog) return;
            var $TempWindow = $($.globalIds.tempWindow);
            $TempWindow.dialog('close');
        },
        openModalDialog: function (opts) {
            var defaults = {
                height: 500,
                width: 500,
                left: undefined,
                top: undefined,
                center: true,
                help: true,
                resizable: false,
                status: true,
                scroll: true,
                dialogHide: undefined, //false,
                edge: undefined, //'raised',
                unadorned: undefined, //false,
                url: '',
                args: undefined,
                cache: false,
                callback: undefined
            }, keyMappings = {
                height: 'dialogHeight',
                width: 'dialogWidth',
                left: 'dialogLeft',
                top: 'dialogTop'
            };

            $.extend(defaults, opts);
            var featureStr = '';
            for (var key in defaults) {
                var value = defaults[key];
                if (defaults[key] === true || defaults[key] === 1)
                    value = 'yes';
                else if (defaults[key] === false || defaults[key] === 0)
                    value = 'no';
                if (keyMappings[key] && defaults[key] != undefined) {
                    featureStr += keyMappings[key] + ':' + value + ';';
                    continue;
                }
                if (defaults[key] != undefined) {
                    featureStr += key + ':' + value + ';';
                }
            }
            if (!defaults.cache) {
                defaults.url += (defaults.url.indexOf('?') > 0 ? '&' : '?') + '__rd=' + (new Date()).getTime();
            }
            var mdReturnValues = window.showModalDialog(defaults.url, self, featureStr);
            if (mdReturnValues && defaults.callback && typeof defaults.callback == 'function')
                defaults.callback.call(null, mdReturnValues);
        }
    });
} (jQuery, undefined));

if (!String.prototype.trim)
    String.prototype.trim = function () {
        var whitespace = "[\\x20\\t\\r\\n\\f]";
        var rTrim = new RegExp("^" + whitespace + "+|((?:^|[^\\\\])(?:\\\\.)*)" + whitespace + "+$", "g");
        return (this + "").replace(rTrim, "");
    }